package com.gxa.community.core.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author bill
 * @date 2022/6/27 11:18
 */
@Component
public class PageUtil<T> {


    private PageInfo<T> pageInfo = null;
    /**
     * @param currentPage
     * @param pageSize
     * @return
     */
    //先得到pageInfo
    public PageInfo getPageInfo(List<T> list, Integer currentPage, Integer pageSize){
        //设置分页条件
        PageHelper.startPage(currentPage,pageSize);
        //查询数据（全查询/条件查询/多表查询）
        //获取getPageInfo
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //获取total总条数
    public Long getTotal(List<T> list, Integer currentPage, Integer pageSize){
        PageInfo pageInfo = this.getPageInfo(list, currentPage, pageSize);
        long total = pageInfo.getTotal();
        return total;
    }

    //获取当前页的数据信息
    public List<T> getCurrentList(List<T> list, Integer currentPage, Integer pageSize){
        PageInfo pageInfo = this.getPageInfo(list, currentPage, pageSize);
        List infoList = pageInfo.getList();
        return infoList;
    }

    //获取总页数
    public Integer getPages(List<T> list, Integer currentPage, Integer pageSize){
        PageInfo pageInfo = this.getPageInfo(list, currentPage, pageSize);
        int pages = pageInfo.getPages();
        return pages;
    }

}
